<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('表格搜索')" />
	<th:block th:include="include :: bootstrap-select-css" />
	<th:block th:include="include :: datetimepicker-css" />
	<th:block th:include="include :: bootstrap-editable-css" />
</head>
<body class="gray-bg">
    <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li class="select-time">
								<label>订单日期：</label>
								<input style="width: 200px;" type="text" class="time-input" id="startTime" autocomplete="off" placeholder="开始时间" name="params[beginDdate]" data-format="yyyy-MM-dd HH:mm:ss"/>
								<span>-</span>
								<input style="width: 200px;" type="text" class="time-input" id="endTime" autocomplete="off" placeholder="结束时间" name="params[endDdate]" data-format="yyyy-MM-dd HH:mm:ss"/>
							</li>
							<li>
								<label>单号：</label>
								<input type="text" name="code"/>
							</li>
							<li>
								<label>单据类型：</label>
								<select name="ctype" >
									<option value="">所有</option>
									<option value="订货">订货</option>
									<option value="退货">退货</option>
								</select>
							</li>
							<li class="select-selectpicker">
								<label>订单状态：</label>
								<select id="st" name="st" class="selectpicker" data-none-selected-text="请选择" multiple>
									<!--<option value="">所有</option>-->
									<option value="保存">保存</option>
									<option value="审核">审核</option>
									<option value="发货中">发货中</option>
									<option value="发货完成">发货完成</option>
									<option value="收货完成">收货完成</option>
									<option value="关闭">关闭</option>
								</select>
							</li>
							<li>
								<label>物流单号：</label>
								<input type="text" name="logcode"/>
							</li>
							<li>
								<label>未发货数量是否为0：</label>
								<select name="unSendqty" id="unSendqty">
									<option value="1111">请选择</option>
									<option value="1">是</option>
									<option value="0">否</option>
								</select>
							</li>
							<li>
								<label>门店：</label>
								<input type="text" name="shopname"/>
							</li>
							<li>
								<label>调出仓库：</label>
								<input type="text" name="fromwhname"/>
							</li>
							<li>
								<label>调入仓库：</label>
								<input type="text" name="towhname"/>
							</li>
							<li>
								<label>物流单号：</label>
								<input type="text" name="logcode"/>
							</li>
							<li>
								<label>打单人员：</label>
								<input type="text" name="emp"/>
							</li>
							<li>
								<label>审核人：</label>
								<input type="text" name="creditemp"/>
							</li>
							<li>
								<label>发货人：</label>
								<input type="text" name="sendemp"/>
							</li>
							<li>
								<label>接受人：</label>
								<input type="text" name="recemp"/>
							</li>
							<li>
								<label>备注：</label>
								<input type="text" name="memo"/>
							</li>
							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>

			<div class="btn-group-sm" id="toolbar" role="group">
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="erp:so:export">
					<i class="fa fa-download"></i> 导出
				</a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table"></table>
			</div>

		</div>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: bootstrap-select-js" />
	<th:block th:include="include :: jquery-cxselect-js" />
	<th:block th:include="include :: datetimepicker-js" />
	<th:block th:include="include :: bootstrap-table-editable-js" />

	<script th:inline="javascript">

		/*$('.time-input').datetimepicker({
            format: 'yyyy-MM-dd HH:mm:ss',
            autoclose: true,
            minView: 0,
            minuteStep:1
        });*/

		// var editFlag = [[${@permission.hasPermi('erp:so:edit')}]];
		var removeFlag = [[${@permission.hasPermi('erp:so:remove')}]];
		var prefix = ctx + "erp/so";
		// var datas = [[${@dict.getType('sys_normal_disable')}]];

		$(function() {
			var options = {
				url: prefix + "/list",
				createUrl: prefix + "/add",
				updateUrl: prefix + "/editSos/{id}",
				// updateSo: prefix + "/edit/{id}",
				removeUrl: prefix + "/remove",
				exportUrl: prefix + "/exportAll",
				modalName: "订单信息",
				showSearch: false,
				showRefresh: false,
				showToggle: false,
				showColumns: false,
				detailView: true,
				uniqueId: "id",
				onExpandRow : function(index, row, $detail) {
					initChildTable(index, row, $detail);
				},
				columns: [{
					checkbox: true
				}
					,
					{
						field: 'id',
						title: '主键',
						formatter: function(value, row, index) {
							return value.toString();
						}
					}
					,
					{
						field: 'ddate',
						title: '订单日期'
					},
					{
						field: 'code',
						title: '单号'
					},
					{
						field: 'ctype',
						title: '单据类型'
					},
					{
						field: 'st',
						title: '订单状态'
					},
					{
						field: 'shopname',
						title: '门店'
					},
					{
						field: 'fromwhname',
						title: '调出仓库'
					},
					{
						field: 'towhname',
						title: '调入仓库'
					},
					{
						field: 'sysdt',
						title: '制单时间'
					},
					{
						field: 'emp',
						title: '打单人员'
					},
					{
						field: 'creditemp',
						title: '审核人'
					},
					{
						field: 'creditdt',
						title: '审核时间'
					},
					{
						field: 'sendemp',
						title: '发货人'
					},
					{
						field: 'senddt',
						title: '发货时间'
					},
					{
						field: 'recemp',
						title: '接受人'
					},
					{
						field: 'recdt',
						title: '接受时间'
					},
					/* {
                         field: 'isreplace',
                         title: '是否厂家代发'
                     },*/
					{
						field: 'memo',
						title: '备注'
					},

					{
						title: '操作',
						align: 'center',
						formatter: function(value, row, index) {
							debugger;
							var actions = [];
						/*	actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="closeso(\'' + row.id + '\')"><i class="fa fa-edit"></i>关闭订单</a> ');*/
							if(row.st == "关闭"){
								actions.push('<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="openso(\'' + row.id + '\')"><i class="fa fa-edit"></i>撤销关闭</a> ');
								actions.push('<a class="btn btn-info btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ')
								return actions.join('');
							}else{

								if(row.ctype == "退货"){
									actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editSo(\'11' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
									if(row.st == "审核"){
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="disReview(\'' + row.id + '\')"><i class="fa fa-edit"></i>撤销审核</a> ');
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="batchUpdate(\'' + row.id + '\')"><i class="fa fa-edit"></i>批量修改发货数量</a> ');
										actions.push('<a class="btn btn-info btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ')
									}else{
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="review(\'' + row.id + '\')"><i class="fa fa-edit"></i>审核</a> ');
									}
								}else {

									if(row.st == "审核"){
										actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="closeso(\'' + row.id + '\')"><i class="fa fa-edit"></i>关闭订单</a> ');
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="disReview(\'' + row.id + '\')"><i class="fa fa-edit"></i>撤销审核</a> ');
										/*actions.push('<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="senditem(\'' + row.id + '\')"><i class="fa fa-edit"></i>确认发货</a>');*/
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="batchUpdate(\'' + row.id + '\')"><i class="fa fa-edit"></i>批量修改发货数量</a> ');
									}else if(row.st == "发货"){
										/*actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="disReview(\'' + row.id + '\')"><i class="fa fa-edit"></i>撤销审核</a> ');*/
										actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="closeso(\'' + row.id + '\')"><i class="fa fa-edit"></i>关闭订单</a> ');
										actions.push('<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="batchUpdate(\'' + row.id + '\')"><i class="fa fa-edit"></i>批量修改发货数量</a> ');
										actions.push('<a class="btn btn-info btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ');
									}else if(row.st == "发货中"){
										/*actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="disReview(\'' + row.id + '\')"><i class="fa fa-edit"></i>撤销审核</a> ');*/
										actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="closeso(\'' + row.id + '\')"><i class="fa fa-edit"></i>关闭订单</a> ');
										actions.push('<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="batchUpdate(\'' + row.id + '\')"><i class="fa fa-edit"></i>批量修改发货数量</a> ');
										actions.push('<a class="btn btn-info btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ');
									}else if(row.st == "保存"){
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="review(\'' + row.id + '\')"><i class="fa fa-edit"></i>审核</a> ');
										actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.editSo(\'11' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
										actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="closeso(\'' + row.id + '\')"><i class="fa fa-edit"></i>关闭订单</a> ');
										/*actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="batchUpdate(\'' + row.id + '\')"><i class="fa fa-edit"></i>批量修改发货数量</a> ');*/
									}else if(row.st == "发货完成"){
										actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="closeso(\'' + row.id + '\')"><i class="fa fa-edit"></i>关闭订单</a> ');
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ');
									}else if(row.st == "收货完成"){
										actions.push('<a class="btn btn-primary btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ');
									}

								}
								return actions.join('');
							}
						}
					}]
			};
			$.table.init(options);
		});
		initChildTable = function(index, row, $detail) {
			//debugger;
			var orderStatus = row.st;
			//alert(row.id);
			var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
			$(childTable).bootstrapTable({
				url: prefix + "/listChild",
				createUrl: prefix + "/add",
				updateUrl: prefix + "/editSos/{id}",
				removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
				modalName: "修改发货数量",
				method: 'post',
				sidePagination: "server",
				contentType: "application/x-www-form-urlencoded",
				queryParams : {
					mid: row.id,
					unSendqty: $("#unSendqty").val()
				},
				columns: [
					{
						field: 'mid',
						title: '表头ID'
					},
					{
						field: 'mcode',
						title: '表头单号'
					},
					{
						field: 'itemcode',
						title: '金蝶编码'
					},{
						field: 'pcode',
						title: '产品代码'
					},
					{
						field: 'pname',
						title: '产品名称'
					},
					{
						field: 'pstd',
						title: '产品规格'
					},
					{
						field: 'punit',
						title: '单位'
					},
					{
						field: 'qty',
						title: '订单数量'
					},
					{
						field: 'sendqty',
						title: '已发货数量'
					},
					{
						field: 'unSendqty',
						title: '未发货数量'
					},
					{
						field: 'smemo',
						title: '备注'
					},
					{
						field: 'sts',
						title: '状态'
					}/*,
                     {
                         title: '操作',
                         align: 'center',
                         formatter: function(value, row, index) {
                             debugger;
                             if((orderStatus == "保存") || (orderStatus="审核")){

                                 var actions = [];
                                 /!*actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'22' + row.id + '\')"><i class="fa fa-edit"></i>修改发货数量</a> ');*!/
                                 actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="addSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>添加发货记录</a> ');
                                 actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="viewSosSend(\'' + row.id + '\')"><i class="fa fa-edit"></i>查看发货记录</a> ');
                                 return actions.join('');

                             }

                         }
                     }*/]
			});

		};

		function batchUpdate(id) {
			$.modal.open("批量修改发货数量", ctx+"erp/sos/view/"+id);
		}

		function viewSosSend(id) {
			$.modal.open("查看发货记录", ctx+"erp/sossend/view/"+id,"1100","500");
		}

		function addSosSend(id) {
			$.modal.open("添加发货记录", ctx+"erp/so/addSosSend/"+id,"800","300");
		}


		function closeso(id) {
			debugger;
			$.ajax({
				type: "post",
				url: ctx + "erp/so/closeSo?id="+id,
				success: function(r) {
					$.modal.msgSuccess("关闭成功");
				}
			});
			/*$.modal.reload();*/
		}

		function openso(id) {
			debugger;
			$.ajax({
				type: "post",
				url: ctx + "erp/so/openSo?id="+id,
				success: function(r) {
					$.modal.msgSuccess("恢复订单成功");
				}
			});
			$.modal.reload();
		}

		function review(id) {
			debugger;
			$.ajax({
				type: "post",
				url: ctx + "erp/so/reviewSo?id="+id,
				success: function(r) {
					$.modal.msgSuccess("审批成功");
				}
			});
			/*$.modal.reload();*/
		}

		function disReview(id) {
			debugger;
			$.ajax({
				type: "post",
				url: ctx + "erp/so/disReviewSo?id="+id,
				success: function(r) {
					$.modal.msgSuccess("撤销成功");
				}
			});
			$.modal.reload();
		}

		function senditem(id) {
			debugger;
			$.ajax({
				type: "post",
				url: ctx + "erp/so/sendOrder?id="+id,
				success: function(r) {
					debugger;
					if(r.msg == "订单审核之后才能发货"){
						$.modal.alertWarning("订单审核之后才能发货");
						$.table.refresh();
					}else{
						$.modal.msgSuccess("发货成功");
						$.table.refresh();
					}
				}
			});
			$.modal.reload();
		}

		// 直接返回获取
		var data = [{"v":"taobao","n":"淘宝","s":[{"v":"tm","n":"天猫"},{"v":"jhs","n":"聚划算"}]},{"v":"jd","n":"京东","s":[{"v":"jdcs","n":"京东超市"},{"v":"jdsx","n":"京东生鲜"}]}];
		$('#element').cxSelect({
			selects: ['type', 'router'],
			jsonValue: 'v',
			data: data
		});

		$('#st').selectpicker('refresh');
		//render方法强制重新渲染引导程序 - 选择ui,如果当您编程时更改任何相关值而影响元素布局，这将非常有用。
		$('#st').selectpicker('render');

	</script>

	<script th:inline="javascript">
	    // 直接返回获取
   		var data = [{"v":"taobao","n":"淘宝","s":[{"v":"tm","n":"天猫"},{"v":"jhs","n":"聚划算"}]},{"v":"jd","n":"京东","s":[{"v":"jdcs","n":"京东超市"},{"v":"jdsx","n":"京东生鲜"}]}];
    	$('#element').cxSelect({
    	  selects: ['type', 'router'],
    	  jsonValue: 'v',
    	  data: data
    	});
    </script>
</body>
</html>
